home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Error Dialogs / ErrorAlert.sit / ErrorCheck.cp next >
Text File  |  1994-02-12  |  6KB  |  176 lines

  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. //  ErrorCheck.cp
  4. //    Copyright 1993 Tim Carroll.  All Rights Reserved.
  5. //
  6. //    Using this code it is possible to report a wide variety of messages
  7. //    to the user using a standardized Alert.
  8. //
  9. //
  10. //  Related Files:
  11. //    
  12. //    ErrorCheck.rsrc contains the minimum number of resources to operate DeathAlert.
  13. //  It also provides a list of standard error strings for simple error reporting.
  14. //
  15. //    ErrorCheck.h must be included by any file that calls these routines.  ErrorCheck.h
  16. //  has one customizable parameter -- the macro SANITY_CHECK.  When SANITY_CHECK is
  17. //  defined, ErrorCheck sends its messages straight to the debugger -- this allows the
  18. //  programmer to define MacsBugs strings, for example.  In addition, other modules
  19. //  can check for this macro and add their own sanity checking code to modules that will
  20. //  only get included when debugging the application.
  21. //
  22. //    Implementation Details:
  23. //
  24. //    In order to maximize its usefulness and minimize the number of function names,
  25. //  ErrorCheck takes advantage of overloaded functions.  You simply pass the appropriate
  26. //  parameters, and it picks the right version of the code to call.
  27. //
  28. //  All versions of ErrorCheck call DebugStr when in SANITY_CHECK mode.  When not in sanity
  29. //  mode, all versions of ErrorCheck call ParamText, and then our standard routine,
  30. //  PostErrorAlert.  PostErrorAlert is sufficiently generic that it shouldn't need to be
  31. //  changed.
  32. //
  33. //  In addition to the string parameters, all versions of ErrorCheck send in a single variable
  34. //  that states whether or not the error was fatal.  A fatal error results in a call to
  35. //  ExitToShell immediately after the dialog is clicked in (or the program is continued).
  36. //
  37. //
  38. //
  39. //  Implementing additional DeathAlerts
  40. //  If you find another way that you want to post a ErrorAlert, you can simply create a new
  41. //  implementation.  It should set up the strings, then follow the calling conventions described
  42. //  above.  All versions should include the DeadOnArrival parameter and call ExitToShell when
  43. //  it is true.
  44. //
  45. //    Keep in mind that you can't have two functions with identical parameters types, as the
  46. //  compiler will not be able to differentiate between the two.
  47. //
  48. //    Thanks to a number of people in various books and magazines who have created similar routines.
  49. //
  50. //  Suggestions for improvements and enhancements are welcomed.  Send E-Mail to
  51. //      TimCarroll@online.apple.com
  52. //
  53. //////////////////////////////////////////////////////////////////////
  54.  
  55. #include "ErrorCheck.h"
  56.  
  57.  
  58. void PostErrorAlert(const Boolean Dead);
  59.  
  60.  
  61. void PostErrorAlert (const Boolean Dead) {
  62.     SetCursor (&qd.arrow);
  63.     if (Dead)
  64.         (void) StopAlert (rDeathAlert, nil);
  65.     else
  66.         (void) StopAlert (rNotDeadYetAlert, nil);
  67. }
  68.  
  69. //  This implementation of DeathAlert is the one I hope will be used the most.
  70. //    All of the error strings are kept in 'STR#' resources.  Ideally, the user
  71. //    will create a separate STR# resource for each major section of code -- this keeps
  72. //    related error messages together.
  73. void ErrorAlert (short ErrorStringsList,
  74.                     short ErrorStringNumber,
  75.                     Boolean DeadOnArrival)
  76. {
  77.     Str255    theMessage;
  78.     
  79.     GetIndString(theMessage, ErrorStringsList, ErrorStringNumber);
  80.  
  81. // The rest of this code can pretty much almost be copied from version to version
  82. //  I kept it separate so I didn't have to pass more variables to PostErrorAlert
  83. // Actually, they could all call the next routine, if I changed it to use references
  84. // instead of sending whole strings....
  85. #ifdef SANITY_CHECK
  86.     DebugStr (theMessage);
  87. #else
  88.     ParamText (theMessage, kEmptyString, kEmptyString, kEmptyString);
  89.     PostErrorAlert (DeadOnArrival);
  90. #endif
  91.     if (DeadOnArrival)
  92.         ExitToShell();
  93. }
  94.     
  95. //    This implementation takes a single string provided by the external
  96. //    program.  It is the best used when the program needs to custom build
  97. //    a string to send to DeathAlert.  Strings should be put into resources
  98. //    whenever possible.
  99. void ErrorAlert (Str255 theMessage,
  100.                         Boolean DeadOnArrival)
  101. {
  102. #ifdef SANITY_CHECK
  103.     DebugStr (theMessage);
  104. #else
  105.     ParamText (theMessage, kEmptyString, kEmptyString, kEmptyString);
  106.     PostErrorAlert (DeadOnArrival);
  107. #endif
  108.     if (DeadOnArrival)
  109.         ExitToShell();
  110. }
  111.  
  112.  
  113. //    This implementation appends a short onto a string --
  114. //  useful for reporting OSErrs, for example.  Note that the SANITY_CHECK version
  115. //  does not send the actual number at this time.  This is a feature and will be
  116. //  fixed later
  117. void ErrorAlert (Str255 theMessage,
  118.                         short ErrorNumber,
  119.                         Boolean DeadOnArrival)
  120. {
  121.     Str255    NumberMessage;
  122.     NumToString (ErrorNumber, NumberMessage);
  123.     
  124. #ifdef SANITY_CHECK
  125.     DebugStr (theMessage);
  126. #else
  127.     ParamText (theMessage, NumberMessage, kEmptyString, kEmptyString);
  128.     PostErrorAlert (DeadOnArrival);
  129. #endif
  130.     if (DeadOnArrival)
  131.         ExitToShell();    
  132.  
  133. }
  134.  
  135. void ErrorAlert (short ErrorStringsList,
  136.                     short ErrorStringNumber,
  137.                     short ErrorNumber, 
  138.                     Boolean DeadOnArrival)
  139. {
  140.     Str255  theMessage;
  141.     Str255    NumberMessage;
  142.     GetIndString(theMessage, ErrorStringsList, ErrorStringNumber);
  143.     NumToString (ErrorNumber, NumberMessage);
  144.  
  145. #ifdef SANITY_CHECK
  146.     DebugStr (theMessage);
  147. #else
  148.     ParamText (theMessage, NumberMessage, kEmptyString, kEmptyString);
  149.     PostErrorAlert (DeadOnArrival);
  150. #endif
  151.     if (DeadOnArrival)
  152.         ExitToShell();
  153. }
  154.  
  155.  
  156. void OSErrCheck (OSErr err) {
  157.     if (err != noErr)
  158.         ErrorAlert (rDefaultStrings, errOSErr, err, false);
  159.     }
  160.  
  161. void MemoryErrCheck (Handle err) {
  162.     if ((!err) || (MemError() != noErr))
  163.         ErrorAlert (rDefaultStrings, errOSErr, MemError(), false);
  164.     }
  165.  
  166. void MemoryErrCheck (Ptr err) {
  167.     if ((!err) || (MemError() != noErr))
  168.         ErrorAlert (rDefaultStrings, errOSErr, MemError(), false);
  169.     }
  170.  
  171. void ResourceErrCheck (Handle err) {
  172.     if ((!err) || (ResError() != noErr))
  173.         ErrorAlert (rDefaultStrings, errOSErr, ResError(), false);
  174.     }
  175.  
  176.